package gemini.castor.ui.client.page.footer;

import gemini.basic.model.User;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class FooterPresenter extends CastorWidgetPresenter<FooterPresenter.Display> {

	public interface Display extends CastorWidgetDisplay {
		void setUsername(String username);
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_WELCOME);

	@Inject
	public FooterPresenter(Display display, EventBus eventBus) {
		super(display, eventBus);
	}

	private String username = "username";
	
	@Override
	protected void onBind() {
		username = Window.prompt("Enter username: ", username);
		if (username != null) {
			ServiceCatalog.getUserService().getUserByName(username,
					new AsyncCallback<User>() {

						@Override
						public void onSuccess(User result) {
							if (result != null) {
								display.setUsername(result.getUsername());
							} else {
								Window.alert("User with name <" + username
										+ "> is not exist in database!");
							}
						}

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Call service failed: " + caught.getMessage());
						}
					});
		}
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
}
